﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AuthTool_Build : System.Web.UI.Page
{
    public string CourseID
    {
        get
        {
            if (Request.QueryString["CourseID"] != null)
                return Request.QueryString["CourseID"];
            return "";
        }
    }

    public const string xmlns = "xmlns=\"http://www.imsglobal.org/xsd/imscc/imscp_v1p1\" xmlns:lomimscc=\"http://ltsc.ieee.org/xsd/imscc/LOM\" xmlns:lom=\"http://ltsc.ieee.org/xsd/LOM\" xmlns:voc=\"http://ltsc.ieee.org/xsd/LOM/vocab\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.imsglobal.org/xsd/imscc/imscp_v1p1 http://www.imsglobal.org/profile/cc/ccv1p0/imscp_v1p2_localised.xsd http://ltsc.ieee.org/xsd/LOM http://www.imsglobal.org/profile/cc/ccv1p0/domainProfile_1/lomLoose_localised.xsd http://ltsc.ieee.org/xsd/imscc/LOM http://www.imsglobal.org/profile/cc/ccv1p0/domainProfile_2/lomLoose_localised.xsd\"";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (CourseID == "")
        {
            btnUpdateManifest.Visible = false;
            btnExportCartridge.Visible = false;
        }
        else
        {
            btnBuildCartridge.Visible = false;
            pnlBuild.Visible = false;
        }
    }

    protected void MakeCartridge(object sender, EventArgs e)
    {
        MakeManifest();
        BuildCartridge("~/TempFiles/" + hidGuid.Value + "/", txtCartridgeName.Text + ".zip");
    }

    private void BuildCartridge(string DN, string FN)
    {
        System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(DN) + FN, System.IO.FileMode.Create);
        using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zos = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(fs))
        {
            zos.UseZip64 = ICSharpCode.SharpZipLib.Zip.UseZip64.Off;
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath(DN));
            foreach (System.IO.DirectoryInfo dfo in di.GetDirectories())
            {
                foreach (System.IO.FileInfo fi in dfo.GetFiles())
                {
                    System.IO.FileStream cf = fi.OpenRead();
                    byte[] buffer = new byte[(int)cf.Length];
                    cf.Read(buffer, 0, buffer.Length);

                    ICSharpCode.SharpZipLib.Zip.ZipEntry ze = new ICSharpCode.SharpZipLib.Zip.ZipEntry(dfo.Name + "/" + fi.Name);
                    ICSharpCode.SharpZipLib.Checksums.Crc32 crc32 = new ICSharpCode.SharpZipLib.Checksums.Crc32();
                    crc32.Reset();
                    crc32.Update(buffer);
                    ze.Crc = crc32.Value;
                    ze.Size = buffer.Length;
                    ze.DateTime = DateTime.Now;

                    zos.PutNextEntry(ze);
                    zos.Write(buffer, 0, buffer.Length);
                }
            }

            foreach (System.IO.FileInfo fi in di.GetFiles())
            {
                if (fi.Name != FN)
                {
                    System.IO.FileStream cf = fi.OpenRead();
                    byte[] buffer = new byte[(int)cf.Length];
                    cf.Read(buffer, 0, buffer.Length);
                    cf.Close();

                    ICSharpCode.SharpZipLib.Zip.ZipEntry ze = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fi.Name);
                    ICSharpCode.SharpZipLib.Checksums.Crc32 crc32 = new ICSharpCode.SharpZipLib.Checksums.Crc32();
                    crc32.Reset();
                    crc32.Update(buffer);
                    ze.Crc = crc32.Value;
                    ze.Size = buffer.Length;
                    ze.DateTime = DateTime.Now;

                    zos.PutNextEntry(ze);
                    zos.Write(buffer, 0, buffer.Length);
                }
            }
        }

        fs.Close();
        Response.Redirect(DN + FN);
    }

    private void MakeManifest()
    {
        string XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<manifest identifier=\"" + txtCartridgeName.Text.Replace(" ", "_") + "\" " + xmlns + ">" + hidXML.Value + "</manifest>";
        System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/TempFiles/" + hidGuid.Value + "/") + "imsmanifest.xml", System.IO.FileMode.Create);
        System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
        sw.Write(XML);
        sw.Close();
        fs.Close();
    }

    protected void UpdateManifest(object sender, EventArgs e)
    {
        string XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<manifest identifier=\"" + txtCartridgeName.Text.Replace(" ", "_") + "\" " + xmlns + ">" + hidXML.Value + "</manifest>";
        System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/Course/Courses/" + CourseID + "/") + "imsmanifest.xml", System.IO.FileMode.Create);
        System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
        sw.Write(XML);
        sw.Close();
        fs.Close();
        lblStatus.ForeColor = System.Drawing.Color.DarkGreen;
        lblStatus.Text = "Manifest Updated";
    }

    protected void ExportCartridge(object sender, EventArgs e)
    {
        string XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<manifest identifier=\"" + txtCartridgeName.Text.Replace(" ", "_") + "\" " + xmlns + ">" + hidXML.Value + "</manifest>";
        System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/Course/Courses/" + CourseID + "/") + "imsmanifest.xml", System.IO.FileMode.Create);
        System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
        sw.Write(XML);
        sw.Close();
        fs.Close();
        lblStatus.ForeColor = System.Drawing.Color.DarkGreen;
        lblStatus.Text = "Manifest Updated.";

        BuildCartridge("~/Course/Courses/" + CourseID + "/", new LMSData.Course(CourseID).CourseName + ".zip");
    }
}
